import {UserActions} from './user';

const initialState = {
  total: 0,
  current: 1,
  pageSize: 10,
  items: {},
  dentistOptions: [],
  clinicOptions: [],
  technicianOptions: [],
  factoryOptions: [],
};

export const OrderStateColor = {
  ordering: '#FF6060',
  confirmen: '#1BBB4D',
  delivery: '#666666',
  received: '#999999',
};

export const OrderState = {
  ORDERING: 'ordering', // 订购  已经创建订单，等待供应商接单
  CONFIRMEN: 'confirmen', // 确认　供应商已经接单等待发货
  REFUSED: 'refused', // 拒绝　供应商已经拒绝订单
  DELIVERY: 'delivery', // 交货　供应商已经发货等待收货
  RECEIVED: 'received', // 收到　诊所已经确认收货等待好评
  PRAISED: 'praised', // 好评　已经好评完成订单
  CANCELLED: 'cancelled', // 取消
  ASSURE: 'assure',
  PAID: 'paid',
};

export const OrderStateName = {
  ordering: 'order.state.ordering',
  confirmen: 'order.state.confirmen',
  refused: 'order.state.refused',
  delivery: 'order.state.delivery',
  received: 'order.state.received',
  praised: 'order.state.praised',
  cancelled: 'order.state.cancelled',
  assure: 'order.state.assure',
  paid: 'order.state.paid',
};

export const OrderProductType = {
  FIXED: 'fixed',
  REMOVABLE: 'removable',
};

export const OrderProductTypeName = {
  fixed: 'product_params_type_fixed',
  removable: 'product_params_type_move',
};

export const OrderListActions = {
  ORDER_LIST_UPDATE: 'order_list_update',
  ORDER_LIST_UPDATE_ITEM: 'order_list_update_item',
  ORDER_LIST_RESET: 'order_list_reset',
  ORDER_LIST_REFRESH: 'order_list_refresh',
  ORDER_LIST_APPEND: 'order_list_append',
};

export function updateOrderList(payload) {
  return {
    type: OrderListActions.ORDER_LIST_UPDATE,
    payload,
  };
}

export function refreshOrderList(payload) {
  return {
    type: OrderListActions.ORDER_LIST_REFRESH,
    payload,
  };
}
export function appendOrderList(payload) {
  return {
    type: OrderListActions.ORDER_LIST_APPEND,
    payload,
  };
}

export function updateOrderListItem(payload) {
  return {
    type: OrderListActions.ORDER_LIST_UPDATE_ITEM,
    payload,
  };
}

export function resetOrderList(payload) {
  return {
    type: OrderListActions.ORDER_LIST_RESET,
  };
}

export default function orderList(state = {...initialState}, action) {
  switch (action.type) {
    case OrderListActions.ORDER_LIST_REFRESH:
      const payload = {};
      action.payload.items.forEach(o => {
        payload[o.id] = o;
      });
      return {
        ...state,
        total: action.payload.total,
        current: 1,
        pageSize: 10,
        items: payload,
      };
    case OrderListActions.ORDER_LIST_APPEND:
      action.payload.items.forEach(o => {
        state.items[o.id] = o;
      });
      return {
        ...state,
        total: action.payload.total,
        current: action.payload.current,
        pageSize: 10,
        items: {...state.items},
      };
    case OrderListActions.ORDER_LIST_UPDATE:
      return {...state, ...action.payload};
    case OrderListActions.ORDER_LIST_UPDATE_ITEM:
      return {
        ...state,
        items: {
          ...state.items,
          [action.payload.id]: action.payload,
        },
      };
    case OrderListActions.ORDER_LIST_RESET:
      return {
        ...initialState,
        total: 0,
        current: 1,
        pageSize: 10,
        items: {},
      };
    case UserActions.USER_SIGN_OUT:
      return {...initialState};
    default:
      return state;
  }
}
